package com.cf.game.cowboy.handler;

import com.cf.game.Player;
import com.cf.game.cowboy.CowBoyGame;
import com.cf.game.cowboy.MessageHandler;
import com.cf.game.cowboy.msg.CowGameInfoResponse;
import com.cf.game.cowboy.msg.CowStakeRequest;
import com.cf.game.cowboy.msg.CowStakeResponse;
import com.cf.game.cowboy.msg.CowUserInfoResponse;
import com.cf.game.service.constants.EnumH5CowBoyGameState;
import com.cf.game.service.constants.EnumReturnCode;
import com.cf.game.service.pojo.UserInfo;
import com.cf.game.service.service.UserInfoService;
import com.cf.game.service.service.h5game.H5GameInstanceService;
import com.cf.game.service.service.h5game.H5GamePayRecordService;
import com.cf.game.service.service.h5game.H5RoundGamePlayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * cf-game-StakeMessageHandler
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/27 14:34
 */
@Slf4j
@Component
public class StakeMessageHandler implements MessageHandler<CowStakeRequest, CowBoyGame> {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private H5GameInstanceService h5GameInstanceService;

    @Autowired
    private H5GamePayRecordService gamePayRecordService;

    @Autowired
    private H5RoundGamePlayRecordService roundGamePlayRecordService;


    @Override
    @Transactional
    public void handle(Player player, CowBoyGame game, CowStakeRequest message) {

        boolean canPlay = EnumH5CowBoyGameState.STAKE.equals(game.state);
        if (!canPlay) {
            //非投注状态,返回游戏状态,客户端同步
            CowGameInfoResponse cowGameInfoResponse = game.gameStateDetail.toCowQueryGameInfoResponse(player.getUserInfo().getId());
            cowGameInfoResponse.setCanPlay(false);
            player.unicast(cowGameInfoResponse);
            return;
        }

        //扣钱
        if (!userStake(player, game, message)) {
            log.info("用户钱包余额不足");
            return;
        }

        //游戏状态包含用户信息

        game.players.forEach((userId, playerSession) -> {
            CowGameInfoResponse infoResponse = game.gameStateDetail.toCowQueryGameInfoResponse(userId);
            infoResponse.setCanPlay(true);
            playerSession.unicast(infoResponse);
        });

        //单播用户余额消息
        CowUserInfoResponse userInfoResponse = new CowUserInfoResponse("获取余额OK", EnumReturnCode.SUCCESS.getCode());
        UserInfo userInfo = userInfoService.getUserInfoByUserId(player.getUserInfo().getId());
        if (userInfo != null) {
            userInfoResponse.setMoney(userInfo.getGameCoin() == null ? 0 : userInfo.getGameCoin());
            player.unicast(userInfoResponse);
        }
    }

    @Transactional
    protected boolean userStake(Player player, CowBoyGame game, CowStakeRequest message) {
        boolean chargingResult = userInfoService.charging(player.getUserInfo().getId(), message.getMoney(), "西部对决扣币");
        if (!chargingResult) {
            //余额不足
            player.unicast(new CowStakeResponse("余额不足！", EnumReturnCode.NEED_RECHARGE_ERROR.getCode(), message.getMoney()));
            return false;
        }
        //增加h5游戏消费记录
        gamePayRecordService.addGamePayRecord(game.getGameId(), game.h5GameInstance.getId(), player.getUserInfo().getId(), message.getMoney());

        synchronized (player) {
            //增加或者更新用户投注总计
            roundGamePlayRecordService.updateOrCreateRoundGamePlayRecord(game.h5GameInstance, player.getUserInfo().getId(), message.getMoney());
        }
        h5GameInstanceService.updateGetMoney(game.h5GameInstance.getId(), message.getMoney());
        //增加游戏状态统计值
        game.gameStateDetail.userAddStake(player.getUserInfo().getId(), message.getGroupId(), message.getStakeId(), message.getMoney());
        return true;
    }

    public boolean support(Class clazz) {
        return CowStakeRequest.class.equals(clazz);
    }


}
